package com.ruoyi.StarStudio.Service.Impl;

import com.ruoyi.StarStudio.DAO.CouponsDAO;
import com.ruoyi.StarStudio.DAO.ExchangeDAO;
import com.ruoyi.StarStudio.Entity.Coupons;
import com.ruoyi.StarStudio.Entity.Exchange;
import com.ruoyi.StarStudio.Entity.Login;
import com.ruoyi.StarStudio.Entity.MessageConstant;
import com.ruoyi.StarStudio.Service.ExchangeService;
import com.ruoyi.common.core.domain.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Service
public class ExchangeServiceImpl implements ExchangeService {

    @Autowired
    private ExchangeDAO exchangeDAO;

    @Autowired
    CouponsDAO couponsDAO;

    @Override
    public List<Exchange> findExchangeList(Exchange exchange) {
        return exchangeDAO.findExchangeList(exchange);
    }

    @Override
    public Exchange findByExchange_id(long id) {
        return exchangeDAO.findByExchange_id(id);
    }

    @Override
    public List<Exchange> findByExchange_user_id(long id) {
        return exchangeDAO.findByExchange_user_id(id);
    }

    @Override
    public void deleteExchangeById(long id) {
        exchangeDAO.deleteExchangeById(id);
    }

    @Override
    public void addExchange(Exchange exchange) {
        exchangeDAO.addExchange(exchange);
    }

    @Override
    public void updateExchange(Exchange exchange) {
        exchangeDAO.updateExchange(exchange);
    }

    @Override
    public long findAllRecordNumber() {
        return exchangeDAO.findAllRecordNumber();
    }

    @Override
    public void updateExchangeStatus(long coupons_item_id, String status,String exchange_store) {
        exchangeDAO.updateExchangeStatus(coupons_item_id, status,exchange_store,new Timestamp(new Date().getTime()));
    }

    @Override
    public Exchange findByCoupons_item_id(long coupons_item_id) {
        return exchangeDAO.findByCoupons_item_id(coupons_item_id);
    }

    @Override
    public SysUser findSysUser(Login login) {
        return exchangeDAO.findSusUserByUsernameAndPassword(login);
    }


    /**
     * 定时检查优惠券是否过期  若过期则更新状态
     * 每隔 1小时检查一次
     */
    @Scheduled(cron = "0 0 */${time.interval} * * ?")
    public void AutoCheckExpiredStatus(){
        List<Long> unExpiredCouponsItemIds = exchangeDAO.findUnExpiredCouponsItemIds(MessageConstant.EXCHANGE_STATUS_EXCHANGED_BUT_NOT_USED);
        if (unExpiredCouponsItemIds.size() > 0){
            for (Long unExpiredCouponsItemId : unExpiredCouponsItemIds) {
                Coupons byCoupons_item_id = couponsDAO.findByCoupons_item_id(unExpiredCouponsItemId);
                if (new Timestamp(new Date().getTime()).after(byCoupons_item_id.getDate_use_end())){
                    //已过期   更新 Exchange状态
                    exchangeDAO.updateExchangeStatusToExpired(unExpiredCouponsItemId,MessageConstant.EXCHANGE_STATUS_EXPIRED);
                }
            }
        }
    }

}
